package com.example.a2032.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.Utils.ContactUtils;
import com.example.a2032.Utils.SchedulersTransformer;
import com.example.a2032.Utils.Utils;
import com.example.a2032.adapter.ContactAdapter;
import com.example.a2032.fragment.OpFragment;
import com.example.a2032.entity.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class TelActivity extends BaseActivity implements ContactAdapter.onItemClickListener {

    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.RecyclerView_Contact)
    RecyclerView RecyclerViewContact;
    private ContactAdapter contactAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tel);
        ButterKnife.bind(this);
        init();
    }

    private void init() {

        String title = getIntent().getStringExtra(OpFragment.KEY_Title);
        tvTitle.setText(title);
        contactAdapter = new ContactAdapter(getAllContact());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerViewContact.setLayoutManager(linearLayoutManager);
        RecyclerViewContact.setAdapter(contactAdapter);
        contactAdapter.setOnItemClickListener(this);

        modifyContact();

    }

    private List<Contact> getAllContact() {
       return ContactUtils.getAllContact(this);
    }

    private void modifyContact() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                Log.i(TAG, "subscribe: ");

             // ContactUtils.deleteAllContact(TelActivity.this);
                emitter.onComplete();
            }
        }).compose(SchedulersTransformer.io2MainObservable()).subscribe(new Observer<Object>() {
                         @Override
                         public void onSubscribe(Disposable d) {

                         }

                         @Override
                         public void onNext(Object o) {

                         }

                         @Override
                         public void onError(Throwable e) {

                         }

                         @Override
                         public void onComplete() {
                             Log.i(TAG, "onComplete: ");
                             contactAdapter.updateData(getAllContact());
                         }
                     }
        );
    }

    private static final String TAG = "TelActivity";

    private void addContact(String phoneNumber, String name) {
        ContactUtils.addContact(this,phoneNumber,name);

    }

    void deleteContact(String contactName) {

        //根据姓名求id
        Uri uri = ContactsContract.RawContacts.CONTENT_URI;
        ContentResolver resolver = getContentResolver();
        String where = ContactsContract.PhoneLookup.DISPLAY_NAME;
        Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data._ID}, where + "=?", new String[]{contactName}, null);
        if (cursor.moveToFirst()) {
            int id = cursor.getInt(0);
            //根据id删除data中的相应数据
            resolver.delete(uri, where + "=?", new String[]{contactName});
            uri = ContactsContract.Data.CONTENT_URI;
            resolver.delete(uri, ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[]{id + ""});
        }


    }

    public boolean isContactExits(String phoneNumber) {
        Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNumber);
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data.DISPLAY_NAME}, null, null, null);
        boolean isExist = cursor.moveToFirst();
        cursor.close();
        return isExist;
    }



    @OnClick(R.id.iv_title)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onItemClick(Contact contact) {
        Utils.setProperty("persist.sys.sos_call", "false");
        String phone = contact.getPhoneNumber();
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        startActivity(intent);
    }
}